﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using VH6I_B4.ProductParsing;
using VH6I_B4.Datawarehouse;

namespace VH6I_B4.BronBestandReaders
{
    class TxtReader : ABronBestandReader
    {
        private StreamReader _srcFile;
        private string _supermarket;
        private ProductParser _productParser;

        public TxtReader(DatawarehouseApi dwh)
            : base(dwh)
        {
        }

        public override void read()
        {
            try
            {
                _productParser = new ProductParser();
                _srcFile = new StreamReader(@"C:\Users\rein\Dropbox\B4\Proftaak\Bronbestanden\KoffieBestandEuro2007.txt", Encoding.Default, true);

                //Read the first line of text
                string row = _srcFile.ReadLine();

                int aantalRegels = 0;
                //Continue to read until you reach end of file
                while (row != null)
                {
                    if (aantalRegels > 1 && !string.IsNullOrEmpty(row) && !row.Contains("CATEGORY - KOFFIE"))
                        ReadData(row);

                    aantalRegels++;

                    //Read the next line
                    row = _srcFile.ReadLine();
                }
            }
            catch (Exception e)
            {
                // shows exception message in console if any errors occured
                System.Diagnostics.Debug.WriteLine("** An error occured while opening the text file, error message: " + e.Message + Environment.NewLine);
            }
            finally
            {
                if (_srcFile != null)
                    //close the file
                    _srcFile.Close();
            }
        }

        protected void ReadData(string row)
        {
            string rawProduct;

            if (row.Length > 118)
                rawProduct = row.Substring(18, 118).Trim();
            else
                rawProduct = row.Substring(18, row.Length - 18).Trim();

            // SUPERMARKET
            if (!" ".Equals(row.Substring(16, 1)))
            {
                _supermarket = row.Substring(15).Trim();
            }
            else if (!string.IsNullOrEmpty(_supermarket) && !" ".Equals(row.Substring(16, 1)))
            {
                _supermarket = row.Substring(15).Trim();
            }

            Dictionary<ProductLevel, string> parserResults = _productParser.Parse(rawProduct);

            if (parserResults.ContainsKey(ProductLevel.BARCODE))
            {
                //PrintLabels(parserResults);

                const char specialChar = (char)8364;

                if (row.Contains((specialChar).ToString()))
                {
                    var rowWithvalues = row.Substring(136);
                    const int start = 0;
                    const int end = 23;
                    double rowValue;

                    if (rowWithvalues.IndexOf("   ") != -1)
                    {
                        if (rowWithvalues.Substring(start, end).Contains(specialChar.ToString()))
                        {
                            if (rowWithvalues.Trim().IndexOf("   ") != -1)
                            {
                                rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1, end).Trim().Substring(1).Trim());

                                _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 1, rowValue);
                            }
                            else
                            {
                                rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                                _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 1, rowValue);
                            }
                        }
                        else
                        {
                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 1, strToDouble(string.Empty));
                        }

                        rowWithvalues = rowWithvalues.Substring(end);
                    }
                    else
                    {
                        if (rowWithvalues.Contains(specialChar.ToString()))
                        {
                            rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 1, rowValue);

                            rowWithvalues = rowWithvalues.Substring(end);
                        }
                    }

                    if (rowWithvalues.IndexOf("   ") != -1)
                    {
                        if (rowWithvalues.Substring(start, end).Contains(specialChar.ToString()))
                        {
                            if (rowWithvalues.Trim().IndexOf("   ") != -1)
                            {
                                rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1, end).Trim().Substring(1).Trim());

                                _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 2, rowValue);
                            }
                            else
                            {
                                rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                                _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 2, rowValue);
                            }
                        }
                        else
                        {
                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 2, strToDouble(string.Empty));
                        }

                        rowWithvalues = rowWithvalues.Substring(end);
                    }
                    else
                    {
                        if (rowWithvalues.Contains(specialChar.ToString()))
                        {
                            rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 2, rowValue);

                            rowWithvalues = rowWithvalues.Substring(end);
                        }
                    }

                    if (rowWithvalues.IndexOf("   ") != -1)
                    {
                        if (rowWithvalues.Substring(start, end).Contains(specialChar.ToString()))
                        {
                            if (rowWithvalues.Trim().IndexOf("   ") != -1)
                            {
                                rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1, end).Trim().Substring(1).Trim());

                                _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 3, rowValue);
                            }
                            else
                            {
                                rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                                _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 3, rowValue);
                            }
                        }
                        else
                        {
                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 3, strToDouble(string.Empty));
                        }

                        rowWithvalues = rowWithvalues.Substring(end);
                    }
                    else
                    {
                        if (rowWithvalues.Contains(specialChar.ToString()))
                        {
                            rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 3, rowValue);

                            rowWithvalues = rowWithvalues.Substring(end);
                        }
                    }

                    if (rowWithvalues.IndexOf("   ") != -1)
                    {
                        if (rowWithvalues.Substring(start, end).Contains(specialChar.ToString()))
                        {
                            rowValue = Convert.ToDouble(rowWithvalues.Substring(start + 1).Trim().Substring(1).Trim());

                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 4, rowValue);
                        }
                        else
                        {
                            _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 4, strToDouble(string.Empty));
                        }
                    }
                }
                else
                {
                    _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 1, strToDouble(string.Empty));
                    _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 2, strToDouble(string.Empty));
                    _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 3, strToDouble(string.Empty));
                    _dwh.SaveFact(FactType.EURO_SALES, parserResults, _supermarket, 2007, 4, strToDouble(string.Empty));
                }
            }
        }

        private void PrintLabels(Dictionary<ProductLevel, string> parserResults)
        {
            if (!string.IsNullOrEmpty(_supermarket))
                // _txtBoxLog.AppendText("[CATEGORY]" + "\t" + _category + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[SUPERMARKET]" + "\t" + _supermarket);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.MAJOR_BRAND]))
                //_txtBoxLog.AppendText("[MAJOR.BRAND]" + "\t" + _majorBrand + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[MAJOR.BRAND]" + "\t" + parserResults[ProductLevel.MAJOR_BRAND]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.APPARAAT]))
                // _txtBoxLog.AppendText("[APPARAAT]" + "\t" + _apparaat + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[APPARAAT]" + "\t\t" + parserResults[ProductLevel.APPARAAT]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.TYPE]))
                //_txtBoxLog.AppendText("[TYPE]" + "\t\t" + _type + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[TYPE]" + "\t\t\t" + parserResults[ProductLevel.TYPE]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.BELEVING]))
                //_txtBoxLog.AppendText("[BELEVING]" + "\t" + _beleving + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[BELEVING]" + "\t\t" + parserResults[ProductLevel.BELEVING]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.SOORT]))
                //_txtBoxLog.AppendText("[SOORT]" + "\t\t" + _soort + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[SOORT]" + "\t\t\t" + parserResults[ProductLevel.SOORT]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.FORMAAT]))
                // _txtBoxLog.AppendText("[FORMAAT]" + "\t" + _formaat + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[FORMAAT]" + "\t\t" + parserResults[ProductLevel.FORMAAT]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.SMAAK]))
                //_txtBoxLog.AppendText("[SMAAK]" + "\t\t" + _smaak + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[SMAAK]" + "\t\t\t" + parserResults[ProductLevel.SMAAK]);

            if (!string.IsNullOrEmpty(parserResults[ProductLevel.BARCODE]))
                //_txtBoxLog.AppendText("[UPC]" + "\t\t" + _upc + Environment.NewLine + Environment.NewLine);
                System.Diagnostics.Debug.WriteLine("[UPC]" + "\t\t\t" + parserResults[ProductLevel.BARCODE] + Environment.NewLine);
        }
    }
}
